package cn.keniykey.mc.landterritory.command.landadmin;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.bukkit.command.CommandSender;

import cn.keniykey.mc.landterritory.LandTerritoryMain;
import cn.keniykey.mc.landterritory.command.AbCommandBase;
import cn.keniykey.mc.landterritory.config.ConfigurationManager;
import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.config.ViewsConfiguration;
import cn.keniykey.mc.landterritory.config.WorldsConfiguration;
import cn.keniykey.mc.landterritory.landflag.LandFlag;
import cn.keniykey.mc.landterritory.landflag.LandFlagManager;

public final class CommandFlag extends AbCommandBase{
	private static final List<String> tablist = new ArrayList<>(Arrays.asList("list","reload","load","unload"));

	CommandFlag() {
		super("flag", "land.command.flag", false);
	}
	
	@Override
	public boolean execute(CommandSender sender, List<String> parameter) {
		final int size = parameter.size();
		if(size == 1) {
			switch(parameter.get(0).toLowerCase()) {
			case "reload":
				sender.sendMessage(LanguageManager.getChatLang(1203131));
				LandFlagManager.getFlagManager().unloadFlags();
				sender.sendMessage(LanguageManager.getChatLang(1203132));
				LandFlagManager.getFlagManager().loadFlags();
				sender.sendMessage(LanguageManager.getChatLang(1203133));
				ConfigurationManager.reloadConfiguration();
				LanguageManager.reloadLanguageConfiguration();
				ViewsConfiguration.reloadViewsConfiguration();
				WorldsConfiguration.reloadWorldsConfiguration();
				sender.sendMessage(LanguageManager.getChatLang(1203111));
				return true;
			case "list":
				final StringBuilder builder = new StringBuilder();
				builder.append(LanguageManager.getChatLang(1203140, LandFlagManager.getFlagManager().getLandFlags().size()));
				builder.append("\n");
				for(final LandFlag landflag : LandFlagManager.getFlagManager().getLandFlags()) {
					builder.append("§e");
					builder.append(landflag.getDescription().getName());
					builder.append("§r, ");
				}
				sender.sendMessage(builder.substring(0, builder.length()-3));
				return true;
			default:
			}
		}
		if(size > 1) {
			final String flagname = parameter.get(1);
			LandFlag flag = LandFlagManager.getFlagManager().getLandFlag(flagname);
			switch(parameter.get(0).toLowerCase()) {
			case "load":
				if(flag != null) {
					sender.sendMessage(LanguageManager.getChatLang(1203141, flag.getName(), flag.getDisplayName()));
					return true;
				}
				flag = null;
				File flagfile = new File(LandTerritoryMain.getPlugin().getDataFolder(),"landflag/" + flagname + ".jar");
				if(!flagfile.exists() || !flagfile.isFile()) {
					sender.sendMessage(LanguageManager.getChatLang(1203142, flagname));
					return true;
				}
				final int lang = (LandFlagManager.getFlagManager().loadFlag(flagfile) ? 1203143 : 1203144);
				sender.sendMessage(LanguageManager.getChatLang(lang, flagname));
				return true;
			case "unload":
				if(flag == null) {
					sender.sendMessage(LanguageManager.getChatLang(1203145, flagname));
					return true;
				}
				LandFlagManager.getFlagManager().unloadFlag(flag);
				sender.sendMessage(LanguageManager.getChatLang(1203146, flagname));
				return true;
			default:
			}
		}
		return false;
	}
	@Override
	public List<String> prompt(CommandSender sender, List<String> parameter) {
		if(parameter.size() == 1) return tablist;
		return emptylist;
	}
	@Override
	public String usage() {
		return LanguageManager.getLang(1203130);
	}
}